有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java设置水平回收视图

我想在“回收者”视图中为我的第一块瓷砖添加10dp填充

就像我的show应用程序中的书一样,第一个应用程序有10dp的填充(假设),然后是两块瓷砖之间的填充(假设5dp)

enter image description here

类似地,最后一个磁贴也有10dp(假定)填充

enter image description here

如果有人想添加一些东西,我将添加我的recycler视图代码 回收站转接器。java

package com.example.安卓.indianmetro;

import 安卓.content.Context;
import 安卓.graphics.Bitmap;
import 安卓.support.annotation.NonNull;
import 安卓.support.v7.widget.RecyclerView;
import 安卓.util.Log;
import 安卓.view.LayoutInflater;
import 安卓.view.View;
import 安卓.view.ViewGroup;
import 安卓.widget.ImageView;
import 安卓.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;

public class RecyclerViewAdapter extends 
RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

private static final String TAG = "RecyclerViewAdapter";

private ArrayList<String> mPlaceNames = new ArrayList<>();
private ArrayList<String> mPlaceImageUrl = new ArrayList<>();
private ArrayList<String> mPlaceMetroDistance = new ArrayList<>();
private ArrayList<String> mClosestMetro = new ArrayList<>();
private Context mContext;

public RecyclerViewAdapter(Context context, ArrayList<String> placeNames, ArrayList<String> placeImageUrl,
                           ArrayList<String> placeMetroDistance, ArrayList<String> closestMetro    ) {
    mPlaceNames = placeNames;
    mPlaceImageUrl = placeImageUrl;
    mPlaceMetroDistance = placeMetroDistance;
    mClosestMetro = closestMetro;
    mContext = context;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.horizontal_item, viewGroup, false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
    Log.d(TAG, "onCreateViewHolder: called");

    Glide.with(mContext)
            .asBitmap()
            .load(mPlaceImageUrl.get(i))
            .into(viewHolder.placeImage);

    viewHolder.placeName.setText(mPlaceNames.get(i));
    viewHolder.placeMetroDistance.setText(mPlaceMetroDistance.get(i));
    viewHolder.closestMetro.setText(mClosestMetro.get(i));

}

@Override
public int getItemCount() {
    if (mPlaceNames.size() < 8){
        return mPlaceNames.size();
    } else return 8;
}

public class ViewHolder extends RecyclerView.ViewHolder{

    ImageView placeImage;
    TextView placeName;
    TextView placeMetroDistance;
    TextView closestMetro;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);

        placeImage = (ImageView) itemView.findViewById(R.id.image);
        placeName = itemView.findViewById(R.id.textView);
        placeMetroDistance = itemView.findViewById(R.id.textView2);
        closestMetro = itemView.findViewById(R.id.placeMetroName);
    }
}
}

我在RecyclerView中使用的瓷砖代码

水平项目。xml

<?xml version="1.0" encoding="utf-8"?>
<安卓.support.constraint.ConstraintLayout
xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content">

<安卓.support.v7.widget.CardView
    安卓:id="@+id/cardView2"
    安卓:layout_width="120dp"
    安卓:layout_height="140dp"
    安卓:layout_marginStart="10dp"
    安卓:layout_marginTop="8dp"
    app:cardCornerRadius="10dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <ImageView
        安卓:id="@+id/image"
        安卓:layout_width="match_parent"
        安卓:layout_height="match_parent"
        安卓:scaleType="centerCrop"
        安卓:src="@drawable/delhi1" />

</安卓.support.v7.widget.CardView>


<TextView
    安卓:id="@+id/textView"
    安卓:layout_width="110dp"
    安卓:layout_height="wrap_content"
    安卓:layout_marginStart="10dp"
    安卓:layout_marginTop="2dp"
    安卓:ellipsize="end"
    安卓:maxLines="1"
    安卓:text="India Gate"
    安卓:textColor="@color/darkHeading"
    安卓:textSize="15sp"
    安卓:textStyle="bold"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/cardView2" />

<TextView
    安卓:id="@+id/textView2"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:layout_marginStart="10dp"
    安卓:layout_marginTop="36dp"
    安卓:text="2.4 Km Away"
    安卓:textColor="@color/blueAccent"
    安卓:textSize="10sp"
    安卓:textStyle="bold"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/cardView2" />

<ImageView
    安卓:id="@+id/imageView2"
    安卓:layout_width="10dp"
    安卓:layout_height="10dp"
    安卓:layout_marginStart="10dp"
    安卓:layout_marginTop="24dp"
    安卓:src="@drawable/ic_metro"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/cardView2" />

<TextView
    安卓:id="@+id/placeMetroName"
    安卓:layout_width="90dp"
    安卓:layout_height="wrap_content"
    安卓:layout_marginLeft="25dp"
    安卓:layout_marginStart="25dp"
    安卓:layout_marginTop="20dp"
    安卓:ellipsize="end"
    安卓:maxLines="1"
    安卓:text="Central Secretariat"
    安卓:textColor="@color/subHeading"
    安卓:textSize="12sp"
    安卓:textStyle="bold"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/cardView2" />

</安卓.support.constraint.ConstraintLayout>

此外,如果有人可以建议如何使瓷砖从左侧完全可见,而不允许任何瓷砖从左侧部分隐藏

enter image description here


共 (1) 个答案

  1. # 1 楼答案

    我想您可以尝试在onBindViewHolder()
    中设置填充 首先:

    if (i == 0) 
        viewHolder.placeImage.setPadding(10, 5, 5, 5);
    

    最后:

    if (i == mPlaceImageUrl.size() - 1) 
        viewHolder.placeImage.setPadding(5, 5, 10, 5);
    

    将数字更改为您喜欢的数字,它们是左->;顶部->;右->;底部填充